﻿using aspNetCURD.Database;
using aspNetCURD.Models;
using aspNetCURD.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace aspNetCURD.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ScController : ControllerBase
    {
        private readonly IScRepository _scRepository;
        private readonly AppDbContext _context;
        public ScController(IScRepository scRepository, AppDbContext context)
        {
            _scRepository = scRepository;
            _context = context;
        }
        /// <summary>
        /// 获取学生成绩信息
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetScs(string keyword)
        {
            var scRepository = _scRepository.GetScs(keyword);
            if(scRepository == null || scRepository.Count() <= 0)
            {
                return NotFound("未找到学生成绩信息");
            }
            return Ok(scRepository);
        }
        /// <summary>
        /// 根据学生学号获取成绩信息
        /// </summary>
        /// <param name="sno"></param>
        /// <returns></returns>
        [HttpGet("{sno}")]
        public IActionResult GetSc([FromRoute] string sno)
        {
            if(string.IsNullOrEmpty(sno))
            {
                return BadRequest("输入为空或请求语法错误");
            }
            var scRepository = _scRepository.GetSc(sno);
            if(scRepository == null)
            {
                return NotFound("未找到学生成绩信息");
            }
            return Ok(scRepository);
        }
        /// <summary>
        /// 新增学生成绩信息
        /// </summary>
        /// <param name="sc"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateSc([FromBody] sc sc)
        {
            if(sc == null)
            {
                return BadRequest("输入为空或请求语法错误");
            }
            try
            {
                _scRepository.AddSc(sc);
                _scRepository.Save();
            }
            catch
            {
                throw;
            }
            return Ok("新增学生成绩成功");
        }
        /// <summary>
        /// 修改学生成绩信息
        /// </summary>
        /// <param name="sno"></param>
        /// <param name="sc"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateSc([FromQuery] string sno,[FromBody] sc sc)
        {
            sno = sno.Trim();
            if (string.IsNullOrEmpty(sno))
            {
                return BadRequest("输入为空或请求语法错误");
            }
            if(sno != sc.sno.Trim())
            {
                return BadRequest("输入为空或请求语法错误");
            }
            _context.Entry(sc).State = EntityState.Modified;
            try
            {
                _context.SaveChanges();
            }
            catch
            {
                if(sc == null)
                {
                    return BadRequest("输入为空或请求语法错误");
                }
                else
                {
                    throw;
                }
            }
            return Ok("修改学生成绩成功");
        }
        /// <summary>
        /// 删除学生成绩信息
        /// </summary>
        /// <param name="sno"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteSc([FromQuery] string sno)
        {
            if (string.IsNullOrEmpty(sno))
            {
                return BadRequest("输入为空或请求语法错误");
            }
            var scRepository = _scRepository.GetSc(sno);
            if (scRepository == null)
            {
                return BadRequest("未找到该资源，或该资源已被删除");
            }
            _context.Entry(scRepository).State = EntityState.Deleted;
            try
            {
                _context.SaveChanges();
            }
            catch
            {
                throw;
            }
            return Ok("删除成功");
        }
    }
}
